<template>
	<view v-if="isShow" style="margin: 20px;">
		<uni-forms :rules="rules" ref="form" :modelValue="formData" style="margin: 10px;">
			<uni-forms-item style="margin-bottom: 10px;" name="times">
				<view style="border-radius: 5px;
				padding: 10px;
				background-color: #d7d7d7; height: 40px;">
					<uni-datetime-picker v-model="formData.times" type="daterange" />
				</view>
			</uni-forms-item>
			<uni-forms-item style="margin-bottom: 0px;">
				<view style="padding: 0px 10px;">
					<uni-data-checkbox v-model="formData.hobby" multiple :localdata="hobbys" />
				</view>
			</uni-forms-item>
			<uni-forms-item label="用车人" name="realname" required>
				<uni-easyinput v-model="formData.realname" placeholder="请输入联系人姓名"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="手机号" name="lxmobile" required>
				<uni-easyinput v-model="formData.lxmobile" placeholder="请输入联系手机号"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="车型" name="vmodel" required>
				<uni-easyinput v-model="formData.vmodel" placeholder="请输入用车车型"></uni-easyinput>
			</uni-forms-item>
			<view style="padding: 20px 10px;">
				<button @click="submit()" type="primary">立即预约租车</button>
			</view>
		</uni-forms>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="进行登录" title="登录" content="请先进项授权登录后再操作"
				@confirm="dialogConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShow: false,
				rules: {
					times: {
						rules: [{
							required: true,
							errorMessage: '请选择用车时间'
						}]
					},
					realname: {
						rules: [{
							required: true,
							errorMessage: '请输入用车人姓名'
						}]
					},
					lxmobile: {
						rules: [{
							required: true,
							errorMessage: '请输入用车人联系手机号'
						}]
					}
				},
				userInfo: [],
				formData: {
					times: [],
					hobby: [],
					times: [],
					realname: '',
					lxmobile: '',
					vmodel: ''
				},
				hobbys: [{
					text: '芝麻免押金',
					value: 0
				}, {
					text: '送车上门',
					value: 1
				}]
			}
		},
		onLoad(e) {
			let _this = this;
			if (typeof e.hobby != 'undefined') {
				let hobby = e.hobby.split(',')
				hobby.forEach((res) => {
					this.formData.hobby.push(Number(res))
				})
				console.log(this.formData.hobby)
			}
			if (typeof e.times != 'undefined') {
				this.formData.times = e.times.split(',')
				console.log(this.formData.times)
			}
			uni.showLoading({
				title: '加载中...'
			})
			setTimeout(function() {
				uni.hideLoading()
				_this.isShow = true
			}, 500)
		},
		onReady() {
			let userInfo = uni.getStorageSync('userInfo');
			if (typeof userInfo == 'undefined' || !userInfo) {
				this.$refs.alertDialog.open()
			} else {
				this.userInfo = userInfo
			}
		},
		methods: {
			dialogConfirm() {
				let _this = this;
				//获取用户信息和AuthCode
				my.getAuthCode({
					scopes: 'auth_user',
					success: function(res) {
						if (typeof res.authCode != 'undefined') {
							uni.showLoading({
								title: '授权中...'
							})
							_this.$httpSend({
								path: '/api/v1/alipay/user/userInfo',
								data: {
									authCode: res.authCode
								},
								success: (data) => {
									uni.hideLoading()
									console.log(data)
									_this.userInfo = data;
									uni.setStorageSync('userInfo', data)
								}
							});
						}
					},
					fail: function(res) {
						uni.showToast({
							title: '授权失败',
							icon: 'error',
						})
					}
				});
			},
			submit() {
				var _this = this;
				let userInfo = uni.getStorageSync('userInfo'); // 从本地缓存读取用户信息
				if (typeof userInfo == 'undefined' || !userInfo) {
					this.$refs.alertDialog.open() // 未登录，弹出登录提示框
					return; // 终止后续操作，强制用户先登录
				} else {
					this.userInfo = userInfo // 已登录，将缓存的用户信息赋值给页面变量
				}
				this.$refs['form'].validate().then(res => { // 表单校验通过后执行的逻辑
					console.log('success', res);
					uni.showLoading({
						title: '用车预约中...'
					})
					_this.$httpSend({
						path: '/api/v1/alipay/home/yongche', // 后端接收预约信息的接口地址
						data: { // 提交的预约数据（从表单中获取）
							times: _this.formData.times.toString(), // 用车时间（数组转字符串）
							hobby: _this.formData.hobby
						.toString(), // 附加服务（如 ["0","1"] → "0,1"，对应“芝麻免押金”“送车上门”）
							realname: _this.formData.realname, // 用车人姓名
							lxmobile: _this.formData.lxmobile, // 联系电话
							vmodel: _this.formData.vmodel // 车型
						},
						complete() { // 请求完成（无论成功/失败）的回调（当前为空，可添加隐藏加载的逻辑）

						},
						success: (res) => { // 请求成功的回调
							uni.showToast({
								title: '你的预约我门已收到，我们会尽快联系你。',
								icon: 'none'
							});
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 500)
						}
					});
				}).catch(err => {
					console.log('err', err);
				})
			}
		}
	}
</script>

<style>

</style>